import { history } from '@/utils/utils'
import { baseUrl } from '@/config'

const goto = ({ $wx, currentTarget }) => {
  const path = currentTarget.dataset.path
  path && history.push(path)
}

const uploadFile = ({ $wx: event }, cb) => {
  const { file } = event.detail

  wx.showLoading({ title: 'loading' })
  // this.fileList1 = [file]
  // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
  wx.uploadFile({
    url: `${baseUrl}/admin/sys-file/upload`, // 仅为示例，非真实的接口地址
    filePath: file.url,
    name: 'file',
    // formData: { user: 'test' },
    success: ({ data: res }) => {
      // 上传完成需要更新 fileList
      const { data } = JSON.parse(res)
      file.fileUrl = data.url

      cb && cb(file)
    },
    fail: () => {},
    complete: () => {
      wx.hideLoading()
    }
  })
}

export default {
  data: {},
  methods: {
    goto,
    uploadFile
  }
}
